package com.zh.demo04.controller;

import com.alibaba.fastjson.JSON;
import com.zh.demo04.common.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zh.demo04.common.ApiResult.success;


/**
 * 公开接口
 *
 * @author ZH
 * @date 13:50 2023/5/28
 */
@Slf4j
@RestController
@RequestMapping({"/api/open", "/admin/open"})
public class HelloController {

    @GetMapping("/hello")
    public ApiResult hello() {
        System.out.println("call hello ...");

        return success("hello world", "success");
    }

    /**
     * 获取 authentication
     *
     * @return
     */
    @GetMapping("/userinfo")
    public ApiResult getUserDetail1() {
        log.info("call getUserDetail1() ...");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null) {
            var principal = authentication.getPrincipal();
            System.out.println("principal = " + JSON.toJSONString(principal));

            return success(principal);
        }
        return success(null, "no authentication");
    }

}
